/*
 * Copyright (c) 2019 Cossack Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "soter/soter_rsa_key.h"

#include <soter/soter_api.h>

SOTER_PRIVATE_API
soter_status_t soter_rsa_pub_key_check_length(const soter_container_hdr_t* key, size_t key_length)
{
    switch (key->tag[3]) {
    case RSA_SIZE_TAG_1024:
        if (key_length == sizeof(soter_rsa_pub_key_1024_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_2048:
        if (key_length == sizeof(soter_rsa_pub_key_2048_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_4096:
        if (key_length == sizeof(soter_rsa_pub_key_4096_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_8192:
        if (key_length == sizeof(soter_rsa_pub_key_8192_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;
    }
    return SOTER_INVALID_PARAMETER;
}

SOTER_PRIVATE_API
soter_status_t soter_rsa_priv_key_check_length(const soter_container_hdr_t* key, size_t key_length)
{
    switch (key->tag[3]) {
    case RSA_SIZE_TAG_1024:
        if (key_length == sizeof(soter_rsa_priv_key_1024_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_2048:
        if (key_length == sizeof(soter_rsa_priv_key_2048_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_4096:
        if (key_length == sizeof(soter_rsa_priv_key_4096_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;

    case RSA_SIZE_TAG_8192:
        if (key_length == sizeof(soter_rsa_priv_key_8192_t)) {
            return SOTER_SUCCESS;
        }
        return SOTER_INVALID_PARAMETER;
    }
    return SOTER_INVALID_PARAMETER;
}
